<?php
//Required for connectDB()
require("dbFunctions.php");

/** 
 * Retrieves all of a Resources details based on the QR code provided.
 *
 * Author: Steve Gennaoui
 * Date: 11/18/2012
 */

//Get the QR Code
$json_qr_codes = $_REQUEST['qr_code'];

$qr_codes = json_decode( $json_qr_codes );

if( is_numeric($qr_codes) )
	$qr_codes = array($qr_codes);
if( is_null($qr_codes) || !isset($qr_codes) )
	die("No QR Codes provided.");

//Database connection
$dbh = connectDB();

foreach($qr_codes as $qr)
{
	//Find the Table name to find the resource details in
	$query = "SELECT qr_code_type FROM QR_Code WHERE qr_code=:qr_code AND is_active=1";
	$stmt = $dbh->prepare($query);
	$stmt->bindParam(':qr_code',$qr);
	$stmt->execute();

	if( $stmt->rowCount() == 1 )
	{
		$table_name = $stmt->fetch(PDO::FETCH_ASSOC);
		$table_name = $table_name['qr_code_type'];
		$query = "SELECT * FROM $table_name WHERE qr_code=:qr_code";

		$stmt = $dbh->prepare($query);
		$stmt->bindParam('qr_code',$qr);
		$stmt->execute();
	
		$results[] = $stmt->fetch(PDO::FETCH_ASSOC);
		
		if( isCurrentlyCheckedOut($qr) )
		{
			$query = "SELECT auxillary_items, comments FROM Resource_Designation WHERE qr_code=:qr_code AND has_been_checked_in=false";
			$stmt = $dbh->prepare($query);
			$stmt->bindParam('qr_code',$qr);
			$stmt->execute();
			
			$res_designation_results[] = $stmt->fetch(PDO::FETCH_ASSOC);
			
			$results[0] = array_merge( $results[0], $res_designation_results[0] );
		}
	}
}

print( json_encode($results) );

$stmt = null;
$dbh = null;

?>
